package com.java.fileman;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.naming.*;
import javax.naming.directory.*;

import java.util.Hashtable;
import java.util.Properties;

public class LdapAuth  {
	
	public static Properties prop;
	public LdapAuth(String appDir){
		prop = new Properties();
		try {
			prop.load(new FileInputStream(appDir+File.separator+"WEB-INF"+File.separator+"config.properties"));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
//	public static void main(String[] args){
//		LdapAuth auth = new LdapAuth("D:\\workspace\\FileServer");
//		try {
//			auth.authenticate("akaushik", "ktintneduts");
//		} catch (ServletException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}
	protected AuthToken authenticate(String username, String password) throws ServletException, IOException {

		final String SUCCESS = "Success.html";
		final String FAILURE = "Failure.html";
		String strUrl = "login.html";


		Hashtable env = new Hashtable(11);

		boolean b = false;

		env.put(Context.INITIAL_CONTEXT_FACTORY,
				"com.sun.jndi.ldap.LdapCtxFactory");
		env.put(Context.PROVIDER_URL, prop.getProperty("auth.ldap.provider"));
		env.put(Context.SECURITY_AUTHENTICATION, prop.getProperty("auth.ldap.level"));
//		env.put(Context.SECURITY_PRINCIPAL, "cn="+username+",dc=columnit,dc=com");
		env.put(Context.SECURITY_PRINCIPAL, username+prop.getProperty("auth.ldap.domain"));
		env.put(Context.SECURITY_CREDENTIALS, password);

		try {
			// Create initial context
			DirContext ctx = new InitialDirContext(env);
			Attributes matchAttrs = new BasicAttributes(true); // ignore attribute name case  
            matchAttrs.put(new BasicAttribute("mail"));  
  
            SearchControls sc = new SearchControls();
            String[] attributeFilter = { "cn", "mail", "OU" };
            sc.setReturningAttributes(attributeFilter);
            sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
            
            String filter = "(&(mail="+username+prop.getProperty("auth.ldap.domain")+"))";
            
            NamingEnumeration<?> answer = ctx.search("", filter, sc);  
            AuthToken authToken = null;
            while (answer.hasMore()) {  
                SearchResult sr = (SearchResult) answer.next();  
                authToken = new AuthToken();
                authToken.setEmail(sr.getAttributes().get("mail").toString());
                authToken.setUsername(username);
                authToken.setName(sr.getAttributes().get("cn").toString());
                return authToken;
            }            
			ctx.close();
			return null;

		} catch (NamingException e) {
//			e.printStackTrace();
			return null;
		} 
	}
}